import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

/**
 * describe:
 *
 * @author huxingjie
 * @date 2021/08/20
 * @since 5.0
 */
public class GzipUtil {
    public static byte[] compress(byte[] bytes) {
        try (ByteArrayOutputStream out = new ByteArrayOutputStream();
             GZIPOutputStream gzip = new GZIPOutputStream(out)) {
            gzip.write(bytes);
            gzip.close();
            return out.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
            return bytes;
        }
    }

    public static byte[] uncompress(byte[] bytes) {
        long begin = System.currentTimeMillis();
        try (ByteArrayOutputStream out = new ByteArrayOutputStream();
             ByteArrayInputStream in = new ByteArrayInputStream(bytes)) {
            GZIPInputStream ungzip = new GZIPInputStream(in);
            byte[] buffer = new byte[1024];
            int n;
            while ((n = ungzip.read(buffer)) >= 0) {
                out.write(buffer, 0, n);
            }
            long end = System.currentTimeMillis();
            return out.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
            return bytes;
        }
    }
}

    